<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="" prop="addtime">
      <el-input v-model="dataForm.addtime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="deletestatus">
      <el-input v-model="dataForm.deletestatus" placeholder=""></el-input>
    </el-form-item> -->
    <el-form-item label="类型" prop="templateId">
      <el-select v-model="dataForm.templateId">
        <el-option label="首页" value="1"></el-option>
        <el-option label="附近商家" value="2"></el-option>
        <el-option label="购物车" value="3"></el-option>
        <el-option label="我的" value="4"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="分类名称" prop="floorName">
      <el-input v-model="dataForm.floorName" placeholder="分类名称"></el-input>
    </el-form-item>
    <!-- <el-form-item label="是否显示" prop="showStatus" v-if="dataForm.id != 0">
      <el-select v-model="dataForm.showStatus" placeholder="是否显示">
          <el-option
            v-for="item in dataForm.displayOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
    </el-form-item> -->
    <el-form-item label="banner背景色" prop="bannerColourValue">
      <el-color-picker v-model="dataForm.bannerColourValue"></el-color-picker>
    </el-form-item>
    <el-form-item label="图标" prop="img1">
      <span><b>苹果手机尺寸</b></span><br/>
      <span>提示：请严格按照尺寸上传图片<span style="font-weight:bold;">&nbsp;上传的图片不得超过1M</span></span>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadBottomIcon?token=${this.$cookie.get('token')}&type=@2x&date=${this.dataForm.date}`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess1"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.img1" :src="dataForm.img1" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span>请上传点击前2x44图片</span>
          <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-col>
        <el-col :span="6">
          <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadBottomIcon?token=${this.$cookie.get('token')}&type=@2x&date=${this.dataForm.date2}`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess2"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.img2" :src="dataForm.img2" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span>请上传点击后2x44图片</span>
          <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-col>
        <el-col :span="6">
          <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadBottomIcon?token=${this.$cookie.get('token')}&type=@3x&date=${this.dataForm.date}`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess3"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.img3" :src="dataForm.img3" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span>请上传点击前3x66图片</span>
          <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-col>
        <el-col :span="6">
          <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadBottomIcon?token=${this.$cookie.get('token')}&type=@3x&date=${this.dataForm.date2}`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess4"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.img4" :src="dataForm.img4" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span>请上传点击后3x66图片</span>
          <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-col>
      </el-row>
      <span><b>安卓手机尺寸</b></span>
      <el-row>
        <el-col :span="6">
          <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadBottomIcon?token=${this.$cookie.get('token')}&type=&date=${this.dataForm.date}`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess5"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.img5" :src="dataForm.img5" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span>请上传点击前图片</span>
          <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-col>
        <el-col :span="6">
          <el-upload
            class="avatar-uploader"
            :action="this.$http.adornUrl(`/banner/uploadBottomIcon?token=${this.$cookie.get('token')}&type=&date=${this.dataForm.date2}`)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess6"
            :before-upload="beforeAvatarUpload">
            <img v-if="dataForm.img6" :src="dataForm.img6" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span>请上传点击后图片</span>
          <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-col>
      </el-row>
    </el-form-item>
    <!-- <el-form-item label="显示顺序" prop="seq">
      <el-input type="number" style="width: 120px" v-model="dataForm.seq" placeholder="倒序显示"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="跳转类型" prop="jumpType">
      <el-row  style="display: flex;">
        <el-select v-model="dataForm.jumpType" placeholder="分类" value="" style="margin-right: 30px">
          <el-option label="无跳转" :value="6"></el-option>
          <el-option label="商品分类" :value="0"></el-option>
          <el-option label="联盟店分类" :value="1"></el-option>
          <el-option label="外链" :value="3"></el-option>
          <el-option label="福利商城" :value="9"></el-option>
          <el-option label="兑换商城" :value="10"></el-option>
          <el-option label="商学院" :value="11"></el-option>
          <el-option label="特殊处理" :value="12"></el-option>
          <el-option label="分享" :value="13"></el-option>
          <el-option label="商城一级分类" :value="17"></el-option>
        </el-select>
        <el-input v-model="dataForm.url" placeholder="请输入跳转类型链接"></el-input>
      </el-row>
    </el-form-item> -->
    <el-form-item label="备注" prop="remark">
      <el-input type="textarea" v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          id: 0,
          date: '',
          date2: '',
          floorName: '',
          templateId: '',
          urlData: '',
          remark: '',
          bannerColourValue: '',
          seq: '',
          img1: '',
          img2: '',
          img3: '',
          img4: '',
          img5: '',
          img6: '',
          path: 'https://image.yilianbaihui.cn/consumer/',
        },
        dataRule: {
          templateId: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          seq: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleAvatarSuccess1 (res, file) {
        this.dataForm.img1 = res.data
        debugger
      },
      handleAvatarSuccess2 (res, file) {
        this.dataForm.img2 = res.data
        debugger
      },
      handleAvatarSuccess3 (res, file) {
        this.dataForm.img3 = res.data
      },
      handleAvatarSuccess4 (res, file) {
        this.dataForm.img4 = res.data
      },
      handleAvatarSuccess5 (res, file) {
        this.dataForm.img5 = res.data
      },
      handleAvatarSuccess6 (res, file) {
        this.dataForm.img6 = res.data
      },
      beforeAvatarUpload (file) {
        console.log(file)
        const isJPG = file.type === 'image/jpeg' || 'image/png'
        // const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        // if (!isLt2M) {
        //   this.$message.error('上传头像图片大小不能超过 2MB!');
        // }
        // return isJPG && isLt2M;
        return isJPG
      },
      init (id) {
        this.dataForm.id = 0
        this.dataForm.date = Date.parse(new Date())
        this.dataForm.date2 = Date.parse(new Date()) + '1'
        console.log(this.dataForm.date)
        console.log(this.dataForm.date2)
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/banner/getBannerById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.floorName = data.data.floorName
                this.dataForm.templateId = data.data.templateId
                this.dataForm.seq = data.data.seq
                this.dataForm.bannerColourValue = data.data.bannerColourValue
                if (data.data.urlData != null && data.data.urlData.length > 0) {
                  let img = []
                  img = data.data.urlData.split(',')
                  this.dataForm.img1 = this.dataForm.path + data.data.imgPath + '/' + img[0] + '@2x'
                  this.dataForm.img2 = this.dataForm.path + data.data.imgPath + '/' + img[1] + '@2x'
                  this.dataForm.img3 = this.dataForm.path + data.data.imgPath + '/' + img[0] + '@3x'
                  this.dataForm.img4 = this.dataForm.path + data.data.imgPath + '/' + img[1] + '@3x'
                  this.dataForm.img5 = this.dataForm.path + data.data.imgPath + '/' + img[0]
                  this.dataForm.img6 = this.dataForm.path + data.data.imgPath + '/' + img[1]
                }
                // this.dataForm.urlData = data.data.urlData
                this.dataForm.remark = data.data.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataForm.bannerColourValue == null || this.dataForm.bannerColourValue === '') {
          this.$message.error('色值不能是空！')
          return
        }
        if (this.dataForm.img1 == null || this.dataForm.img1 === '' || this.dataForm.img1.length < 8) {
          this.$message.error('2x44点击前图片不能为空！')
          return
        }
        if (this.dataForm.img2 == null || this.dataForm.img2 === '' || this.dataForm.img2.length < 8) {
          this.$message.error('2x44点击后图片不能为空！')
          return
        }
        if (this.dataForm.img3 == null || this.dataForm.img3 === '' || this.dataForm.img3.length < 8) {
          this.$message.error('3x66点击前图片不能为空！')
          return
        }
        if (this.dataForm.img4 == null || this.dataForm.img4 === '' || this.dataForm.img14length < 8) {
          this.$message.error('3x66点击后图片不能为空！')
          return
        }
        if (this.dataForm.img5 == null || this.dataForm.img5 === '' || this.dataForm.img5.length < 8) {
          this.$message.error('安卓点击前图片不能为空！')
          return
        }
        if (this.dataForm.img6 == null || this.dataForm.img6 === '' || this.dataForm.img6.length < 8) {
          this.$message.error('安卓点击后图片不能为空！')
          return
        }
        let filePath = ''
        filePath = this.dataForm.img5.substring(this.dataForm.img5.lastIndexOf('/') + 1)
        filePath = filePath +',' + this.dataForm.img6.substring(this.dataForm.img6.lastIndexOf('/') + 1)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/banner/${!this.dataForm.id ? 'addBottomIcon' : 'modifyBottomIcon'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'seq': this.dataForm.templateId,
                'bannerType': 25,
                'templateId': this.dataForm.templateId,
                'floorName': this.dataForm.floorName,
                'urlData': filePath,
                'remark': this.dataForm.remark,
                'bannerColourValue': this.dataForm.bannerColourValue
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
  }
  .avatar {
    width: 128px;
    height: 128px;
    display: block;
  }
</style>
